/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.particle;

import com.android1.amarena2d.annotations.Incomplete;
import com.android1.amarena2d.commons.Tools;
import com.android1.amarena2d.nodes.behavior.LifecyleTick;
import com.android1.amarena2d.nodes.particle.effects.RadialParticleEffect;
import com.badlogic.gdx.graphics.Color;

import static com.android1.amarena2d.commons.Tools.randomMinus1To1;


@Incomplete
public class RadialEmitter extends Emitter<RadialParticle> implements LifecyleTick {

    protected final RadialParticleEffectConfig config;

    public RadialEmitter(RadialParticleEffect effect, int effectLevel, float x, float y) {
        this(effect, effectLevel, x, y, 0, 0);
    }

    public RadialEmitter(RadialParticleEffect effect, int effectLevel, float x, float y, float width, float height) {
        super(effect, effectLevel, x, y, width, height);
        this.config = effect.getEffectConfig();
    }


    @Override
    protected RadialParticle[] createParticles(int count) {
        RadialParticle[] particles = new RadialParticle[count];

        for (int i = 0; i < particles.length; i++) {

            RadialParticle particle = particles[i] = new RadialParticle(i);
            initParticle(particle);

        }
        return particles;
    }


    final Color end = new Color(Color.WHITE);

    @Override
    protected void initParticle(RadialParticle particle) {

        final float degress = (config.angle + config.angleVar * randomMinus1To1()); 
        float a = Tools.degreesToRadians(degress);

        particle.timeToLive = Math.max(0, this.config.life + this.config.lifeVar * randomMinus1To1());

        particle.angle = a;

        particle.degreesPerSecond = Tools.degreesToRadians(config.rotatePerSecond + config.rotatePerSecondVar * randomMinus1To1());

        particle.color.r = config.startColor.r + config.startColorVar.r * randomMinus1To1();
        particle.color.g = config.startColor.g + config.startColorVar.g * randomMinus1To1();
        particle.color.b = config.startColor.b + config.startColorVar.b * randomMinus1To1();
        particle.color.a = config.startColor.a + config.startColorVar.a * randomMinus1To1();

        particle.startColor.set(particle.color);

        end.r = config.endColor.r + config.endColorVar.r * randomMinus1To1();
        end.g = config.endColor.g + config.endColorVar.g * randomMinus1To1();
        end.b = config.endColor.b + config.endColorVar.b * randomMinus1To1();
        end.a = config.endColor.a + config.endColorVar.a * randomMinus1To1();

        particle.deltaColor.r = (end.r - particle.color.r) / particle.timeToLive;
        particle.deltaColor.g = (end.g - particle.color.g) / particle.timeToLive;
        particle.deltaColor.b = (end.b - particle.color.b) / particle.timeToLive;
        particle.deltaColor.a = (end.a - particle.color.a) / particle.timeToLive;

        particle.startSize = config.startSize + config.startSizeVar * randomMinus1To1();

        if (config.endSize == ParticleEffectConfig.SAME_AS_START)
            particle.deltaSize = 0;
        else {
            final float endS = Math.max(0, config.endSize + config.endSizeVar * randomMinus1To1());
            particle.deltaSize = (endS - particle.startSize) / particle.timeToLive;
        }

        particle.startRotation = config.startSpin + config.startSpinVar * randomMinus1To1();
        if (config.endSpin == ParticleEffectConfig.SAME_AS_START)
            particle.deltaRotation = 0;
        else {
            final float endA = config.endSpin + config.endSpinVar * randomMinus1To1();
            particle.deltaRotation = (endA - particle.startRotation) / particle.timeToLive;
        }

        final float startRadius = config.startRadius + config.startRadiusVar * randomMinus1To1();
        final float endRadius = config.endRadius + config.endRadiusVar * randomMinus1To1();

        particle.startRadius = startRadius;


        if (endRadius == ParticleEffectConfig.SAME_AS_START)
            particle.deltaRadius = 0;
        else
            particle.deltaRadius = (endRadius - startRadius) / particle.timeToLive;
    }


    @Override
    protected void update(RadialParticle p, float d) {

        p.angle += p.degreesPerSecond * d;
        p.radius += p.deltaRadius * d;

        /*
        	    p->pos.x = - cosf(p->mode.B.angle) * p->mode.B.radius;
				p->pos.y = - sinf(p->mode.B.angle) * p->mode.B.radius;
         */
        p.pos.x = p.startPos.x +(-(float) Math.cos(p.angle) * p.radius);
        p.pos.y =  p.startPos.y +(-(float) Math.sin(p.angle) * p.radius);

        p.color.r += (p.deltaColor.r * d);
        p.color.g += (p.deltaColor.g * d);
        p.color.b += (p.deltaColor.b * d);
        p.color.a += (p.deltaColor.a * d);

        p.life -= d;

        p.size += Math.max(0, p.deltaSize * d);

        p.rotation += (p.deltaRotation * d);
    }

    @Override
    protected void resetParticle(RadialParticle p) {
        p.startPos.x = this.getPosVarX() * randomMinus1To1() + this.position.x;
        p.startPos.y = this.getPosVarY() * randomMinus1To1() + this.position.y;
        p.radius = p.startRadius;
        p.life = p.timeToLive;
        p.color.set(p.startColor);
        p.size = p.startSize;
        p.rotation = p.startRotation;
    }

    @Override
    public RadialParticleEffectConfig getParticleEffectConfig() {
        return config;
    }

}